$(function () {
    //获取当前用户信息
        var userName = "";
        $.ajax({
            url: "/user/currentuser",
            type: "GET",
            async: false,
            success: function (result) {
                if (result.status == "200") {
					
                    userName = result.data.name;
                } else {

                }
            }

        });
        $("#username span").text(userName);
    	/*显示用户信息下面的时间*/
    	$(".main-sidebar .text-muted").text(moment().format('YYYY-MM-DD HH:mm:ss'));
    	setInterval(function (){
    		$(".main-sidebar .text-muted").text(moment().format('YYYY-MM-DD HH:mm:ss'));
    	},1000);
    	
    	// toggle-btn 头部左侧边栏切换按钮 
    	$(".sidebar-toggle").click(function () {
    	    $(".toggle-btn").toggleClass("on");
    	    $(".bottombar").toggleClass("bottombar-toggle", 400, "easeOutSine");
	    	    if ($(".toggle-btn").hasClass("on")) {
	    	        $(".user-panel").css({"height": "44px", "min-height": "44px"});
	    	        $(".user-panel .small-user").show();
	    	    }else {
	    	        $(".user-panel .small-user").hide();
	    	        $(".user-panel").css({"height": "80px", "min-height": "80px"});
	    	    }
    	});

//    	渲染组织
    function manageRender() {

        $.ajax({
            type: 'get',
            url: '/organization/data/list',
            dataType: 'html',
            success: function (data) {
                    var data = JSON.parse(data);
                    for (var i = 0; i < data.length; i++) {
                        dataId = data[i].id;
	                    if(i==0){
	                    	$('.orgMenu').append('<li class="treeview" id="firstManageRender' + dataId + '"><a href="#"><i class="fa  fa-map-marker"></i><span> ' + data[i].orgName + ' </span><span class="pull-right-container addPull"></span></a><ul class="treeview-menu"></ul></li>')
	                        $("#firstManageRender" + data[i].id + " .addPull").html('<i class="fa fa-plus pull-right" id="addManage' + dataId + '"></i>');
	                        addOrg();
	                    } else{
	                        $("#firstManageRender" + data[i].parent.id + ">ul").append('<li class="orgSelected" id="firstManageRender' + dataId + '"><a href="#"><i class="fa-circle-change fa fa-circle-o"></i><span id="secondManageRender'+ dataId + '"> ' + data[i].orgName + ' </span><span class="pull-right-container addPull"></span></a><ul class="treeview-menu"></ul></li>');
	                        $("#firstManageRender" + data[i].id + " .addPull").html('<i class="fa fa-plus pull-right" id="addManage' + dataId + '"></i>');
	                        addOrg();
	                    }
                    }

            }
        })
    }

    manageRender();
    
    function addOrg() {
        $('.orgMenu').on('click', "#addManage" + dataId, function () {
             $('.modalForm').attr('id','"add'+$(this)[0].id+'"');
            $('.addbtnPrimary').attr('form','"add'+$(this)[0].id+'"');
            $('#firstEditModal').modal('show');
            $('.modalForm input[name="parentid"]').val(this.id.substr(9));
//            return false;
        });
    }

//  提交被点击组织的ID
    $('.orgMenu').on('click', 'li>a', function () {
        var clickId = this.parentNode.id.substr(17);
        $.ajax({
            url: '/organization/edit/id/' + clickId,
            type: 'GET',
            success: function (result) {
            	console.log(result)
                $("#orgList").html(result);
            }
        });
    })

//添加组织
    $('.modalForm').bootstrapValidator({
        excluded: [],
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        /*设置校验属性*/
        fields: {
            orgid: {
                validators: {
                    notEmpty: {
                        message: '请输入组织编号'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_]+$/,
                        message: '组织编号只能包含大写、小写、数字和下划线'
                    }
                }
            },
            orgName: {
                validators: {
                    notEmpty: {
                        message: '请输入组织名称'
                    }
                }
            },
            centrex: {
                validators: {
                    notEmpty: {
                        message: '请输入中心点 X'
                    },
                    regexp: {
                        regexp: /^[0-9]+([.]{1}[0-9]+){0,1}$/,
                        message: '请输入正确坐标'
                    }
                }
            },
            centrey: {
                validators: {
                    notEmpty: {
                        message: '请输入中心点 Y'
                    },
                    regexp: {
                        regexp: /^[0-9]+([.]{1}[0-9]+){0,1}$/,
                        message: '请输入正确坐标'
                    }
                }
            },
            linkMain: {
                validators: {
                    notEmpty: {
                        message: '请输入联系人'
                    }
                }
            },
            linkPhone: {
                validators: {
                    notEmpty: {
                        message: '请输入联系人电话'
                    },
                    regexp: {
                        regexp: /^[0-9]*$/,
                        message: '请输入正确电话号码'
                    }
                }
            },
            principal: {
                validators: {
                    notEmpty: {
                        message: '请输入负责人'
                    }
                }
            },
            principalPhone: {
                validators: {
                    notEmpty: {
                        message: '请输入负责人电话'
                    },
                    regexp: {
                    	regexp: /^[0-9]*$/,
                        message: '请输入正确电话号码'
                    }
                }
            },
        }
    }).off('success.form.bv').on('success.form.bv', function (e) {
        var $form = $(e.target);
        $.ajax({
            type: 'post',
            url: '/organization/save',
            data: $form.serialize(),
            dataType: 'json',
            success: function (data) {
                if (data.status == 200) {
                	
//                    $('#editModalForms input[name="id"]').val(data.data.id);
//                    /*关闭模态框*/
//                     $('#firstEditModal').modal('hide');
//                    /*重置表单*/
//                     $form.data('bootstrapValidator').resetForm(); 
//                      $form.find('input').val('');
                    window.location.reload();
                      
                }else{
                	winTip.alert(data.message);
                }
            }
        });
    });
 // 点击组织列表选中组织显示圆圈
    $(".orgMenu").on("click",".orgSelected a",function(){
    	$(".orgSelected .fa-circle-change").removeClass("fa-circle").addClass("fa-circle-o");
    	$(this).find(".fa-circle-change").removeClass("fa-circle-o").addClass("fa-circle");
		if(!$(this).siblings("ul").html()){
			$(this).siblings("ul").remove();
		}
    	$(".orgSelected a").removeClass("orgSelectedStyle");
    	$(this).addClass("orgSelectedStyle");
    });
//    $(".skin-blue .sidebar-menu1>li").on('mouseover',function (){
//    	console.log(!$(this).hasClass("active"))
//    	if(!$(this).hasClass("active")){
//    		$(this).find("a").css('border-left','3px solid #505050');
//    	}else{
//    		$(this).parent().siblings(".sidebar-menu1").find("a").css('border-left','3px solid #333');
//    	}
//    	
//    });
//    $(".skin-blue .sidebar-menu1>li").on('mouseout',function (){
//    	if(!$(this).hasClass("active")){
//    		$(this).find("a").css('border-left','3px solid #333');
//    	}else{
//    		$(this).parent().siblings(".sidebar-menu1").find("a").css('border-left','3px solid #333');
//    	}
//    	
//    });
//    $(".skin-blue .sidebar-menu1>li").on('click',function (){
//    	$(this).find("a").css('border-left','3px solid #3398db');
//    	$(this).parent().siblings(".sidebar-menu1").find("a").css('border-left','3px solid #333');
//    	$(this).parent().siblings(".sidebar-menu1").removeClass("active");
//    })
})





